//
//  DiscountsViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "DiscountsViewController.h"

#import "YouHuiQuanCell.h"
#import "HBHShowCouponView.h"
@interface DiscountsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSArray *dataArr;//数据
@property (nonatomic,assign)NSInteger pageNumber;//页码
@end

@implementation DiscountsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"我的优惠券"];
    _dataArr = [NSArray new];
    [self.view addSubview:self.tableV];
    [self getCoupons];
}
-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 150*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YouHuiQuanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YouHuiQuanCell"];
    if (!cell) {
        cell = [[YouHuiQuanCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YouHuiQuanCell"];
    }
    cell.backgroundColor = [UIColor clearColor];
    [cell loadCellWithData:_dataArr[indexPath.row]];
    [cell setDataDic:_dataArr[indexPath.row]];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(NSString *couponId){
        HBHShowCouponView *view = [[HBHShowCouponView alloc]initWithCoupon:weakSelf.dataArr[indexPath.row]];
        [view showOnView:weakSelf.view];
    };
    return cell;
}

-(void)getCoupons
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/business/myCoupons" Dic:@{@"jdUser.id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    _dataArr = responseDic[@"data"];
                    [_tableV reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}



@end
